package com.vm.action.mobile;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.Preparable;
import com.vm.action.ActionSuper;
import com.vm.common.Page;
import com.vm.common.Tools;
import com.vm.dao.mobile.base.IMobileClientDao;
import com.vm.entity.MobileClient;
import com.vm.log4j.CustomizeLog4j;

public class MobileClientAction extends ActionSuper implements Preparable {
	
	private static Logger log = CustomizeLog4j.getLogger(MobileClientAction.class);
	
	private Page<MobileClient> page;
	private MobileClient mobileClient;
	private IMobileClientDao mobileClientDao;
	private String pageLinkList;
	
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	
	
	public void prepare() throws Exception {
		//atuo
	}
	
	public String list(){
		this.page = this.mobileClientDao.page(this.PAGE_ROW_SIZE, super.getPageNumber());
		this.pageLinkList =super.getSplitPageLink("/mobile/MobileClientAction_list.do?", this.page);
		return SUCCESS;
	}
	
	public String add() throws Exception {
		if(this.mobileClient != null){
			mobileClient = null;
		}
		return SUCCESS;
	}

	public String save() throws Exception {
		String[] msgArg = new String[] { "MobileClientAction_list.do"};
		String versionNo = String.valueOf(System.currentTimeMillis());
		mobileClient.setKhBbh(versionNo);
		String msg = null;
		try {
			String path = this.saveFile();
			mobileClient.setKhApkurl(path);
			
			mobileClientDao.save(mobileClient);
			msg = super.getResourceMessage("insert_client_success", msgArg);
		} catch (Exception e) {
			log.error("MobileClientAction save mobileClient error  ", e);
			msg = super.getResourceMessage("insert_client_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}

	public String edit(){
		mobileClient = mobileClientDao.findById(this.mobileClient.getKhId());
		return SUCCESS;
	}

	public String update() throws Exception {
		String[] msgArg = new String[] { "MobileClientAction_list.do"};
		String msg = null;
		try {
			String path = this.saveFile();
			mobileClient.setKhApkurl(path);
			
			mobileClientDao.update(mobileClient);
			msg = super.getResourceMessage("update_client_success", msgArg);
		} catch (Exception e) {
			log.error("MobileClientAction save mobileClient error  ", e);
			msg = super.getResourceMessage("update_client_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;

	}
	
	public void updateStatus() throws Exception{
		
	}
	
	public String delete() {
		String[] msgArg = new String[] { "MobileClientAction_list.do"};
		String msg = null;
		try {
			mobileClientDao.deleteById(this.mobileClient.getKhId());
			msg = super.getResourceMessage("delete_client_success", msgArg);
		} catch (Exception e) {
			log.error("MobileClientAction delete mobileClient error  ", e);
			msg = super.getResourceMessage("delete_client_err", msgArg);
		}
		super.setShowMessage(msg);
		return super.SHOW_MESSAGE_RESULT;
	}
	
	public String saveFile() throws Exception{
		String path = "";
		if(this.getUploadContentType() == null){
			return path;
		}
		String fileName = getSavePath() + File.separator +getUploadFileName();
		FileOutputStream fos = new FileOutputStream(fileName);
		FileInputStream fis = new FileInputStream(this.getUpload());
		byte[] b = new byte[1024];
		int len = 0;
		while((len = fis.read(b)) > 0){
			fos.write(b, 0, len);
		}
		path = Tools.getFileUrl(ServletActionContext.getRequest(), this.savePath+"/"+getUploadFileName());
		return path;
		
	}

	public Page<MobileClient> getPage() {
		return page;
	}

	public void setPage(Page<MobileClient> page) {
		this.page = page;
	}

	public MobileClient getMobileClient() {
		return mobileClient;
	}

	public void setMobileClient(MobileClient mobileClient) {
		this.mobileClient = mobileClient;
	}

	public IMobileClientDao getMobileClientDao() {
		return mobileClientDao;
	}

	public void setMobileClientDao(IMobileClientDao mobileClientDao) {
		this.mobileClientDao = mobileClientDao;
	}

	public String getPageLinkList() {
		return pageLinkList;
	}

	public void setPageLinkList(String pageLinkList) {
		this.pageLinkList = pageLinkList;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	
}
